//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.server.impl;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBSort;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.query.DataQuery.CACHE_UNIT;
import com.farm.core.sql.result.DataResult;
import com.farm.core.time.TimeTool;
import com.farm.doc.dao.DocpubmessageDaoInter;
import com.farm.doc.domain.Docpubmessage;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.doc.server.PubmessageServiceInter;
import com.farm.doc.server.UsermessageServiceInter;
import com.farm.doc.server.PubmessageServiceInter.APPTYPE;
import com.farm.doc.server.commons.DocMessageCache;
import com.farm.parameter.FarmParameterService;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PubmessageServiceImpl implements PubmessageServiceInter {
    @Resource
    private DocpubmessageDaoInter docpubmessageDaoImpl;
    @Resource
    private UsermessageServiceInter usermessageServiceImpl;
    private static final Logger log = Logger.getLogger(PubmessageServiceImpl.class);
    @Resource
    private FarmFileManagerInter farmFileServer;

    public PubmessageServiceImpl() {
    }

    @Transactional
    public Docpubmessage sendAnswering(String content, String title, APPTYPE apptype, String appid, LoginUser sendUser) {
        Docpubmessage message = new Docpubmessage();
        message.setCtime(TimeTool.getTimeDate14());
        message.setCuser(sendUser.getId());
        message.setCusername(sendUser.getName());
        message.setAppid(appid);
        message.setContent(content);
        message.setPcontent(apptype.getVar());
        message.setPstate("1");
        message.setReadstate("0");
        message.setTitle(title);
        message.setReaduserid("NONE");
        message.setCritcismnum(Integer.valueOf(0));
        message.setPraisnum(Integer.valueOf(0));
        message.setReplynum(Integer.valueOf(0));
        if (apptype.equals(APPTYPE.USER)) {
            this.usermessageServiceImpl.sendMessage(appid, content, "收到一条留言");
        }

        apptype.equals(APPTYPE.DOCGROUP);
        return this.docpubmessageDaoImpl.insertEntity(message);
    }

    @Transactional
    public DataResult getMessages(String docid, int num, int pagesize) {
        DataQuery dbQuery = DataQuery.init(new DataQuery(), "FARM_DOCPUBMESSAGE A LEFT JOIN ALONE_AUTH_USER C ON C.ID=A.READUSERID LEFT JOIN ALONE_AUTH_USER D ON A.CUSER = D.ID", "A.ID AS ID,A.CTIME AS CTIME,A.CUSERNAME AS CUSERNAME,A.CUSER AS CUSER,A.PSTATE AS PSTATE,A.PCONTENT AS PCONTENT,A.READUSERID AS READUSERID,C.NAME AS READUSERNAME,A.CONTENT AS CONTENT,A.TITLE AS TITLE,A.APPID AS APPID,A.READSTATE AS READSTATE, D.IMGID AS IMGID, A.PRAISNUM AS PRAISNUM, A.CRITCISMNUM AS CRITCISMNUM");
        dbQuery.addRule(new DBRule("APPID", docid, "="));
        dbQuery.addSqlRule(" AND A.PARENTID IS NULL ");
        dbQuery.addSort(new DBSort("CTIME", "DESC"));
        dbQuery.setCurrentPage(num);
        dbQuery.setPagesize(pagesize);
        DataResult result = null;

        try {
            result = dbQuery.search();
            return result;
        } catch (SQLException var7) {
            log.error(var7.toString());
            return DataResult.getInstance();
        }
    }

    @Transactional
    public Docpubmessage approveOf(String id, LoginUser loginUser) {
        Docpubmessage entity = this.docpubmessageDaoImpl.getEntity(id);
        if (!DocMessageCache.getInstance().add(loginUser.getId(), entity.getAppid(), entity.getId())) {
            return entity;
        } else {
            entity.setPraisnum(entity.getPraisnum().intValue() + 1);
            this.docpubmessageDaoImpl.editEntity(entity);
            return entity;
        }
    }

    @Transactional
    public Docpubmessage oppose(String id, LoginUser loginUser) {
        Docpubmessage entity = this.docpubmessageDaoImpl.getEntity(id);
        if (!DocMessageCache.getInstance().add(loginUser.getId(), entity.getAppid(), entity.getId())) {
            return entity;
        } else {
            entity.setCritcismnum(entity.getCritcismnum().intValue() + 1);
            this.docpubmessageDaoImpl.editEntity(entity);
            return entity;
        }
    }

    @Transactional
    public void reply(String content, String appid, String prentMessageId, LoginUser loginUser) {
        Docpubmessage parententity = this.docpubmessageDaoImpl.getEntity(prentMessageId);
        parententity.setReplynum(parententity.getReplynum().intValue() + 1);
        this.docpubmessageDaoImpl.editEntity(parententity);
        Docpubmessage entity = new Docpubmessage();
        entity.setCtime(TimeTool.getTimeDate14());
        entity.setCuser(loginUser.getId());
        entity.setCusername(loginUser.getName());
        entity.setPstate("1");
        entity.setPcontent(parententity.getPcontent());
        entity.setReadstate("1");
        entity.setTitle("回复");
        entity.setReaduserid(loginUser.getId());
        entity.setContent(content);
        entity.setAppid(appid);
        entity.setPraisnum(Integer.valueOf(0));
        entity.setCritcismnum(Integer.valueOf(0));
        entity.setReplynum(Integer.valueOf(0));
        entity.setParentid(prentMessageId);
        this.docpubmessageDaoImpl.insertEntity(entity);
        if (APPTYPE.USER.getVar().equals(entity.getPcontent()) && !parententity.getAppid().equals(loginUser.getId())) {
            this.usermessageServiceImpl.sendMessage(parententity.getAppid(), "关于留言《" + parententity.getContent() + "》的回复信息：" + content, "收到一条留言回复信息");
        }

        if (APPTYPE.DOCGROUP.getVar().equals(entity.getPcontent()) && !parententity.getCuser().equals(loginUser.getId())) {
            this.usermessageServiceImpl.sendMessage(parententity.getCuser(), "小组<a class='color_a' href='webgroup/Pubshow.do?groupid=" + parententity.getAppid() + "'>关于留言《" + parententity.getContent() + "》的回复信息：" + content, "收到一条留言回复信息");
        }

    }

    @Transactional
    public List<Map<String, Object>> getReplys(String docid, String docMessageId) {
        try {
            DataQuery dbQuery = DataQuery.init(new DataQuery(), "FARM_DOCPUBMESSAGE A ", "A.ID AS ID, A.CONTENT AS CONTENT,CTIME,CUSERNAME,CUSER");
            dbQuery.addRule(new DBRule("APPID", docid, "="));
            dbQuery.addRule(new DBRule("PARENTID", docMessageId, "="));
            dbQuery.addSort(new DBSort("CTIME", "DESC"));
            dbQuery.setCurrentPage(1);
            dbQuery.setPagesize(1000);
            return dbQuery.search().getResultList();
        } catch (SQLException var4) {
            log.error(var4.toString());
            return new ArrayList();
        }
    }

    @Transactional
    public DataResult getDocmessageByUser(String userid, Integer pagesize, Integer pagenum) throws SQLException {
        DataQuery dbQuery = DataQuery.getInstance(pagenum.intValue(), "a.id as id,a.CTIME as CTIME,a.CUSERNAME as CUSERNAME,a.CUSER as CUSER,a.PSTATE as PSTATE,a.PCONTENT as PCONTENT,a.READUSERID as READUSERID,a.CONTENT as CONTENT,a.TITLE as TITLE,a.APPID as APPID,a.READSTATE as READSTATE,B.TITLE as DOCTITLE,B.ID as DOCID,A.PRAISNUM as PRAISNUM,A.CRITCISMNUM as CRITCISMNUM,A.REPLYNUM AS REPLYNUM", "FARM_DOCPUBMESSAGE a left join farm_doc b on a.APPID=b.ID");
        dbQuery.addRule(new DBRule("a.CUSER", userid, "="));
        dbQuery.addSqlRule("and b.id is not null");
        dbQuery.setPagesize(pagesize.intValue());
        return dbQuery.search();
    }

    @Transactional
    public DataResult getNewPublicDocMessages(int pagenum) {
        DataQuery dbQuery = DataQuery.init(new DataQuery(), "FARM_DOCPUBMESSAGE A LEFT JOIN ALONE_AUTH_USER C ON C.ID=A.READUSERID LEFT JOIN ALONE_AUTH_USER D ON A.CUSER = D.ID left join FARM_DOC E on e.ID=a.APPID left join FARM_RF_DOCTYPE F on f.DOCID=e.ID left join FARM_DOCTYPE G on G.ID=F.TYPEID", "A.ID AS ID,A.CTIME AS CTIME,A.CUSERNAME AS CUSERNAME,A.CUSER AS CUSER,A.PSTATE AS PSTATE,A.PCONTENT AS PCONTENT,A.READUSERID AS READUSERID,C.NAME AS READUSERNAME,A.CONTENT AS CONTENT,A.TITLE AS TITLE,A.APPID AS APPID,A.READSTATE AS READSTATE, D.IMGID AS IMGID, A.PRAISNUM AS PRAISNUM, A.CRITCISMNUM AS CRITCISMNUM,A.REPLYNUM as REPLYNUM,E.ID as DOCID,E.TITLE as DOCTITLE");
        dbQuery.addSqlRule(" AND A.PARENTID IS NULL AND (e.READPOP='1' and G.READPOP='0')");
        dbQuery.addSort(new DBSort("CTIME", "DESC"));
        dbQuery.setCache(Integer.valueOf(FarmParameterService.getInstance().getParameter("config.wcp.cache.immediately")).intValue(), CACHE_UNIT.second);
        dbQuery.setPagesize(pagenum);
        DataResult result = null;

        try {
            result = dbQuery.search();
        } catch (SQLException var6) {
            log.error(var6.toString());
            return DataResult.getInstance();
        }

        Iterator var5 = result.getResultList().iterator();

        while(var5.hasNext()) {
            Map<String, Object> map = (Map)var5.next();
            if (map.get("IMGID") != null) {
                map.put("IMGURL", this.farmFileServer.getFileURL(map.get("IMGID").toString()));
            }
        }

        result.runformatTime("CTIME", "yyyy-MM-dd HH:mm");
        return result;
    }

    @Transactional
    public void del(String id, LoginUser loginUser) {
        this.docpubmessageDaoImpl.deleteEntity(this.docpubmessageDaoImpl.getEntity(id));
    }

    @Transactional
    public Docpubmessage getEntity(String id) {
        return this.docpubmessageDaoImpl.getEntity(id);
    }
}
